package com.kiwilss.aopdemo.ui.paging

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.paging.PagedList
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.recyclerview.widget.LinearLayoutManager
import com.kiwilss.aopdemo.R
import kotlinx.android.synthetic.main.activity_paging.*

/**
 * @author : Administrator
 * e-mail : kiwilss@163.com
 * time   : 2021/02/02
 *  desc   :
 */
class PagingActivity: AppCompatActivity(R.layout.activity_paging) {
    var page = 1
    val pageSize = 10
    val prefetchDistance = 10

    val mAdapter by lazy { PagingAdapter() }

    val mData by lazy { ArrayList<PagingBean>() }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        //设置分页配置
//        val pageConfig = PagedList.Config.Builder()
//            .setPageSize(pageSize)
//            .setPrefetchDistance(prefetchDistance)
//            .setEnablePlaceholders(true)//ui占位符
//            .build()

        rvPagingList?.run {
            layoutManager = LinearLayoutManager(this@PagingActivity)
            adapter = mAdapter
        }

        //刷新
        btnRefresh.setOnClickListener {
            page = 1
            mData.clear()
            getData()
            mAdapter.refresh()
        }

        //加载更多
        btnNext.setOnClickListener {
            page ++
            getData()
            //mAdapter.submitData(this,pagingData = PagingData)
        }



    }
    fun getData(){
        var pagingBean: PagingBean
        for (i in 0..pageSize){
            pagingBean = PagingBean("name = $page + $i")
            mData.add(pagingBean)
        }
    }

}